--[[
        Sida's Auto Carry - Reborn
        Lucian plugin by fbragequit
        v1.0.2(.1? Toy's Version)
]]
 
if myHero.charName ~= "Lucian" then return end
require 'Prodiction'
 
AutoCarry.PluginMenu:addParam("sep0", "-- Misc Options --", SCRIPT_PARAM_INFO, "")
AutoCarry.PluginMenu:addParam("quickE", "Quick E Cast", SCRIPT_PARAM_ONKEYDOWN, false, string.byte("E"))
AutoCarry.PluginMenu:addParam("sep1", "-- Autocarry Options --", SCRIPT_PARAM_INFO, "")
AutoCarry.PluginMenu:addParam("useQ", "Use - Piercing Light", SCRIPT_PARAM_ONOFF, true)
AutoCarry.PluginMenu:addParam("useQf", "Use - Full Range Q", SCRIPT_PARAM_ONOFF, true)
AutoCarry.PluginMenu:addParam("useW", "Use - Ardent Blaze", SCRIPT_PARAM_ONOFF, true)
AutoCarry.PluginMenu:addParam("useWf", "Use - W with Passive", SCRIPT_PARAM_ONOFF, true)
AutoCarry.PluginMenu:addParam("wReset", "Only Use W after AA", SCRIPT_PARAM_ONOFF, true)
AutoCarry.PluginMenu:addParam("sep2", "-- Mixed Mode Options --", SCRIPT_PARAM_INFO, "")
AutoCarry.PluginMenu:addParam("qHarass", "Use - Piercing Light", SCRIPT_PARAM_ONOFF, true)
AutoCarry.PluginMenu:addParam("useQf2", "Use - Full Range Q", SCRIPT_PARAM_ONOFF, true)
AutoCarry.PluginMenu:addParam("sep3", "-- Drawing Options --", SCRIPT_PARAM_INFO, "")
AutoCarry.PluginMenu:addParam("qDraw", "Draw - Q helpers", SCRIPT_PARAM_ONOFF, false)
AutoCarry.PluginMenu:addParam("drawQ", "Draw - Piercing Light", SCRIPT_PARAM_ONOFF, false)
AutoCarry.PluginMenu:addParam("drawR", "Draw - The Culling", SCRIPT_PARAM_ONOFF, false)
 
 
-- values are from Riot Decoder
qTargetRange, qHitRange, qWidth, qDelay = 570, 1100, 65, 350
wRange, wSpeed, wWidth, wDelay = 1000, 1.6, 80, 300
pBuff = "lucianpassivebuff"
AARange = 550
 
function PluginOnLoad()
        AutoCarry.Crosshair:SetSkillCrosshairRange(1000)
        SkillQ = AutoCarry.Skills:NewSkill(false, _Q, qTargetRange, "Piercing Light", AutoCarry.SPELL_TARGETED, 0, false, false, math.huge, qDelay, qWidth, false)
        SkillW = AutoCarry.Skills:NewSkill(false, _W, wRange, "Ardent Blaze", AutoCarry.SPELL_LINEAR, 0, false, false, wSpeed, wDelay, wWidth, true)
       
        EM = AutoCarry.EnemyMinions()
        PM = ProdictManager.GetInstance()
        qProd = PM:AddProdictionObject(_Q, qHitRange, math.huge, qDelay/1000, qWidth)
       
        AdvancedCallback:bind('OnGainBuff', OnGainBuff)
        AdvancedCallback:bind('OnLoseBuff', OnLoseBuff)
        pFlag = false
end
 
function PluginOnTick()
        qReady = myHero:CanUseSpell(_Q) == READY
        wReady = myHero:CanUseSpell(_W) == READY
        eReady = myHero:CanUseSpell(_E) == READY
        Target = AutoCarry.Crosshair:GetTarget(true)
       
        if Target then
               
                if AutoCarry.Keys.AutoCarry then
                        if GetDistance(Target) > AARange then
                                if wReady and AutoCarry.PluginMenu.useWf then SkillW:Cast(Target) end
                                if qReady and AutoCarry.PluginMenu.useQf then qFarCast() end
                        elseif not pFlag then
                                if qReady and AutoCarry.PluginMenu.useQ then SkillQ:Cast(Target) end
                                if wReady and AutoCarry.PluginMenu.useW then SkillW:Cast(Target) end
                        end
                end
               
                if AutoCarry.Keys.MixedMode then
                        if GetDistance(Target) > AARange then
                                if qReady and AutoCarry.PluginMenu.useQf2 then qFarCast() end
                        else
                                if qReady and AutoCarry.PluginMenu.qHarass then SkillQ:Cast(Target) end
                        end
                end
               
        end
        if AutoCarry.PluginMenu.wReset then AutoCarry.PluginMenu.useW = false end
end
 
function OnAttacked()
        if AutoCarry.Keys.AutoCarry and AutoCarry.PluginMenu.wReset then SkillW:Cast(Target) end
end
 
function qFarCast()
        EM:update()
        Pos = qProd:GetPrediction(Target)
        if not Pos then Pos = Target end
        -- this sometimes bugged and Pos was nil. weird
       
        V = Vector(Pos) - Vector(myHero)
       
        Vn = V:normalized()
        Distance = GetDistance(Pos)
        tx, ty, tz = Vn:unpack()
        TopX = Pos.x - (tx * Distance)
        TopY = Pos.y - (ty * Distance)
        TopZ = Pos.z - (tz * Distance)
       
        Vr = V:perpendicular():normalized()
        Radius = GetDistance(Target, Target.minBBox)
        tx, ty, tz = Vr:unpack()
       
        LeftX = Pos.x + (tx * Radius)
        LeftY = Pos.y + (ty * Radius)
        LeftZ = Pos.z + (tz * Radius)
        RightX = Pos.x - (tx * Radius)
        RightY = Pos.y - (ty * Radius)
        RightZ = Pos.z - (tz * Radius)
       
        Left = WorldToScreen(D3DXVECTOR3(LeftX, LeftY, LeftZ))
        Right = WorldToScreen(D3DXVECTOR3(RightX, RightY, RightZ))
        Top = WorldToScreen(D3DXVECTOR3(TopX, TopY, TopZ))
        Poly = Polygon(Point(Left.x, Left.y), Point(Right.x, Right.y), Point(Top.x, Top.y))
       
        if AutoCarry.PluginMenu.qDraw then
                DrawLeft, DrawRight, DrawTop = Left, Right, Top
                -- making copies for the draw function to let it nil these safely
        end
       
        for i, minion in pairs(EM.objects) do
                toScreen = WorldToScreen(D3DXVECTOR3(minion.x, minion.y, minion.z))
                toPoint = Point(toScreen.x, toScreen.y)
                if Poly:contains(toPoint) then
                        SkillQ:Cast(minion)
                end
        end
end
 
function OnGainBuff(unit, buff)
        if unit.isMe and buff.name == pBuff then
                pFlag = true
        end
end
 
function OnLoseBuff(unit, buff)
        if unit.isMe and buff.name == pBuff then
                pFlag = false
        end
end
 
function PluginOnWndMsg(msg,key)
        if msg == KEY_DOWN and AutoCarry.PluginMenu.quickE then CastSpell(_E, mousePos.x, mousePos.z) end
end
 
function PluginOnDraw()
        if DrawLeft and DrawRight and DrawTop then
                DrawLine(Left.x, Left.y, Right.x, Right.y, 1, 0xFFFF0000)
                DrawLine(Left.x, Left.y, Top.x, Top.y, 1, 0xFFFF0000)
                DrawLine(Right.x, Right.y, Top.x, Top.y, 1, 0xFFFF0000)
                DrawLeft, DrawRight, DrawTop = nil
        end
        if not myHero.dead then
                if qReady and AutoCarry.PluginMenu.drawQ then
                        DrawCircle(myHero.x, myHero.y, myHero.z, 700, 0xFFFFFF)
                end
                if rReady and AutoCarry.PluginMenu.drawR then
                        DrawCircle(myHero.x, myHero.y, myHero.z, 1400, 0xFF0000)
                end
        end
end